package com.example.controller;


import com.example.dto.AirspaceDemandIdQueryBDTO;
import com.example.dto.AirspaceDemandPageQueryBDTO;
import com.example.dto.AirspaceDemandPageQueryDTO;
import com.example.dto.AirspaceDemandReviewDTO;
import com.example.entity.AirspaceDemand;
import com.example.entity.AirspaceDemandMini;
import com.example.service.AirspaceDemandService;
import com.example.util.MicroResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Optional;

@Controller
@RequestMapping("/admin/airspaceDemand")
@Slf4j
//@Api(tags = "空域需求管理")
/**
 * 空域需求管理
 */
public class AirspaceDemandController {

    @Autowired
    private AirspaceDemandService airspaceDemandService;
    /**
     * 功能：分页查询与关键词模糊查询空域需求信息,
     * 输入：pageNum,pageSize keyText, order
     */
    @PostMapping("/keywordSearch")
    public ResponseEntity page(@RequestBody AirspaceDemandPageQueryDTO airspaceDemandPageQueryDTO) {
        log.info("空域需求管理分页查询");
        log.info("空域需求管理分页查询参数:{}", airspaceDemandPageQueryDTO);
        AirspaceDemandPageQueryBDTO<AirspaceDemandMini> page = airspaceDemandService.page(airspaceDemandPageQueryDTO);
        return ResponseEntity.ok(MicroResponseUtils.success(page));
    }
    /**
     * 功能：根据id查询空域需求信息
     * 输入：id
     */
    @PostMapping("/getById")
    public ResponseEntity getByApplicationId(@RequestParam String applicationId) {
        log.info("根据id查询空域需求信息");
        log.info("根据id查询空域需求信息参数:{}", applicationId);
        Optional<AirspaceDemandIdQueryBDTO> airspaceDemandIdQueryBDTO = airspaceDemandService.getByApplicationId(applicationId);
        ResponseEntity responseEntity;
        if(airspaceDemandIdQueryBDTO.isPresent()){
//            responseEntity = new ResponseEntity<>(user.get(), HttpStatus.OK);
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(airspaceDemandIdQueryBDTO.get()));
        }
        else{
            responseEntity = ResponseEntity.ok(MicroResponseUtils.error("User not found"));
        }
        return responseEntity;
    }
    /**
     * 功能：根据id修改空域需求审批状态
     * 输入：AirspaceDemandReviewDTO
     */
    @PostMapping("/updateApprovalStatus")
    public ResponseEntity updateApprovalStatus(@RequestBody AirspaceDemandReviewDTO airspaceDemandReviewDTO) {
        log.info("根据id修改空域需求审批状态");
        log.info("根据id修改空域需求审批状态参数:{}", airspaceDemandReviewDTO);
        Optional<AirspaceDemand> airspaceDemand = airspaceDemandService.update(airspaceDemandReviewDTO);
        ResponseEntity responseEntity;
        if (airspaceDemand.isPresent())
        {
            responseEntity= ResponseEntity.ok(MicroResponseUtils.success(airspaceDemand.get()));
        }
        else
        {
            responseEntity= ResponseEntity.ok(MicroResponseUtils.error("update failed"));
        }
        return responseEntity;
    }

}
